import React, {Component} from 'react';
import {withRouter, Switch, Route, Redirect} from "react-router-dom";
import {deleteUserInfo} from "../../../util";
import {Button, Card, Modal} from "antd";
import {ExclamationCircleOutlined, LogoutOutlined} from '@ant-design/icons';
import Home from "../Home";
import BlogEdit from "../BlogEdit";
import BlogList from "../BlogList";
import Types from "../Types";
import Tags from "../Tags";
import FriendLink from "../FriendLink";
import Message from "../Message";
import ImgFile from "../ImgFile";
import Resources from "../Resources";

const titleMap = {
    "/admin/home": "首页",
    "/admin/blogEdit": "博客编辑",
    "/admin/blogList": "博客列表",
    "/admin/types": "分类管理",
    "/admin/tags": "标签管理",
    "/admin/messages": "留言管理",
    "/admin/friendLink": "友链",
    "/admin/images": "文件管理",
    "/admin/resources": "网盘资源"
}

class Main extends Component {

    logout = () => {
        let {history} = this.props;
        Modal.confirm({
            title: '退出登录',
            icon: <ExclamationCircleOutlined />,
            content: '确认退出吗？',
            okText: '确认',
            cancelText: '取消',
            onOk: () => {
                deleteUserInfo();
                history.replace("/login");
            }
        });
    }

    render() {
        let {pathname} = this.props.location;
        return (
            <div className="site-layout-background">
                <Card title={titleMap[pathname]} extra={<Button
                    icon={<LogoutOutlined />}
                    onClick={this.logout}>退出登录</Button>} >
                    <Switch>
                        <Route path="/admin/home" component={Home}/>
                        <Route path="/admin/blogEdit" component={BlogEdit}/>
                        <Route path="/admin/blogList" component={BlogList}/>
                        <Route path="/admin/types" component={Types}/>
                        <Route path="/admin/tags" component={Tags}/>
                        <Route path="/admin/messages" component={Message}/>
                        <Route path="/admin/friendLink" component={FriendLink}/>
                        <Route path="/admin/images" component={ImgFile}/>
                        <Route path="/admin/resources" component={Resources}/>
                        <Redirect to="/admin/home"/>
                    </Switch>
                </Card>
            </div>
        );
    }
}

export default withRouter(Main);
